﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Brunch
{
	[Serializable]
	public class BrunchConfig
	{
		public BrunchConfig()
		{
			ProjectConfigs = new List<ProjectConfig>();
		}

		public List<ProjectConfig> ProjectConfigs { get; set; }

		internal ProjectConfig GetProjectConfigForPath(string solutionFilename)
		{
			if (string.IsNullOrEmpty(solutionFilename)) return null;

			foreach (var cfg in ProjectConfigs)
			{
				if (solutionFilename.ToUpper().IndexOf(cfg.RootPath.ToUpper()) != -1)
					return cfg;
			}
			return null;
		}

		internal bool SolutionHasConfig(string solutionFilename)
		{
			return GetProjectConfigForPath(solutionFilename) != null;
		}

		internal static ProjectConfig GetProjectConfig(string solutionFilename, string selectedCrumb)
		{
			string[] chunks = solutionFilename.Split(Path.DirectorySeparatorChar);
			string rootPath = string.Empty;
			string projectName = string.Empty;

			//reconstruct root path, based on full path and breadcrumb
			//also remember the parent folder, to propose project name based on in
			for(int i=0; i<chunks.Length; i++)
			{
				if (chunks[i].ToUpper() == selectedCrumb.ToUpper())
				{
					if (i > 0)
						projectName = chunks[i - 1];
					break;
				}
				rootPath += chunks[i] + Path.DirectorySeparatorChar;
			}

			ProjectConfig prjCfg = new ProjectConfig();
			prjCfg.ProjectName = projectName;
			prjCfg.RootPath = rootPath;

			return prjCfg;
		}
	}
}
